package com.tianye.lc.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.tianye.lc.R;
import com.tianye.lc.activity.AddOrShowOrEditActivity;
import com.tianye.lc.activity.BaseActivity;
import com.tianye.lc.common.Final;

import java.util.List;

/**
 * Created by yangyazhou on 2016/12/20.
 */

public class BreedRecyclerAdapter extends RecyclerView.Adapter<BreedRecyclerAdapter.ListViewHolder> {
    private BaseActivity mActivity;
    private List<AVObject> mData;

    public BreedRecyclerAdapter(BaseActivity activity) {
        mActivity = activity;
    }

    public void setData(List<AVObject> data) {
        mData = data;
        notifyDataSetChanged();
    }


    @Override
    public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListViewHolder(mActivity.getLayoutInflater().inflate(R.layout.list_item_breed, parent, false));
    }

    @Override
    public void onBindViewHolder(ListViewHolder holder, int position) {
        AVObject object = mData.get(position);
        holder.name.setText(object.getString("name"));
    }

    @Override
    public int getItemCount() {
        if (null == mData) {
            return 0;
        }
        return mData.size();
    }


    public class ListViewHolder extends RecyclerView.ViewHolder {
        private TextView name;

        public ListViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    Intent intent = new Intent(mActivity, AddOrShowOrEditActivity.class);
                    intent.putExtra(Final.ROSE_ID,mData.get(position).getObjectId());
                    intent.putExtra(Final.IS_SHOW,true);
                    mActivity.startActivity(intent);
                }
            });
        }
    }

}
